package com.fsc.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.fsc.entity.ResultWrapper;
import com.fsc.entity.domain.StudentInfo;
import com.fsc.service.LoginInfoService;
import com.fsc.service.StuPartTeachService;
import com.fsc.service.StudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fsc._enum.ReturnEnum.Not_Found;
import static com.fsc._enum.ReturnEnum.SUCCESS;


@RestController
@RequestMapping("/student")
public class StudentInfoController {
    @Autowired
    private StudentInfoService studentInfoService;

    @Autowired
    private LoginInfoService loginInfoService;
    //查询学生的id和姓名
    @GetMapping
    public ResultWrapper<List<StudentInfo>> getAllStudents() {
        return ResultWrapper.success(SUCCESS, studentInfoService.getAllStudents());
    }

    // 根据教师ID或家长ID获取学生信息
    @GetMapping("/by-teacher-or-parent")
    public ResultWrapper<List<StudentInfo>> getStudentsByCurrentUser() {
        Integer loginId = Integer.parseInt((String) StpUtil.getLoginId());
        int role = loginInfoService.getRole(loginId);  // 获取用户角色的方法
        List<StudentInfo> studentInfos;

        if (role == 1) {
            studentInfos = studentInfoService.getStudentsByTeacherId(loginId);
        } else if (role == 2) {
            studentInfos = studentInfoService.getStudentsByParentId(loginId);
        } else if (role==0) {
            studentInfos = studentInfoService.getAllStudents();
        } else {
            return ResultWrapper.fail(Not_Found);
        }

        return ResultWrapper.success(SUCCESS, studentInfos);
    }
    @GetMapping("/role")
    public ResultWrapper<Map<String, Object>> getRoleAndId() {
        Integer loginId = Integer.parseInt((String) StpUtil.getLoginId());
        int role = loginInfoService.getRole(loginId);
        Map<String, Object> data = new HashMap<>();
        data.put("role", role);
        data.put("id", loginId);
        if (role == 3) {
            String studentName = studentInfoService.getStudentNameById(loginId);
            data.put("name", studentName);
        }
        return ResultWrapper.success(SUCCESS, data);
    }
}
